package ua.com.cms.models;

import com.google.code.morphia.Morphia;
import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import com.mongodb.Mongo;
import org.bson.types.ObjectId;
import ua.com.cms.entities.Slot;
import ua.com.cms.entities.SlotContent;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SlotDao extends BasicDAO<Slot, ObjectId> {
    public SlotDao(Mongo mongo, Morphia morphia, String dbName, String username, String password, boolean isAuthenticated) {
        super(mongo, morphia, dbName);
    }

    public Slot findSlotbyName(String name){
        Query<Slot> query = getDatastore().createQuery(Slot.class);
        query = query.field("name").equal(name);
        if (query!=null){
        	return query.get();
        }
        return null;
    }
    
    public List<Slot> getAll(){
    	return getDatastore().find(Slot.class).asList();
    }

    public Slot findSlotbyId(ObjectId id){
        Query<Slot> query = getDatastore().createQuery(Slot.class);
        return query.field("id").equal(id).get();
    }

    public Slot get_or_create(String name, List<Integer> size, HashMap<String, String> var_args, SlotContent content){
        Slot slot = findSlotbyName(name);
        if(slot == null){
            slot = new Slot();
            slot.setName(name);
            slot.setContent(content);
            slot.setSize(size);
            slot.setVar_args(var_args);
            getDatastore().save(slot);
        }
        return slot;
    }

    public void deleteSlotbyName(String name){
        Query<Slot> query = getDatastore().createQuery(Slot.class);
        query = query.field("name").equal(name);
        getDatastore().delete(query);
    }

    public void deleteSlotbyId(ObjectId id){
        Query<Slot> query = getDatastore().createQuery(Slot.class);
        query = query.field("id").equal(id);
        getDatastore().delete(query);
    }

    public void updateSlotbyId(ObjectId currentId, String name, List<Integer> size, HashMap<String, String> var_args, SlotContent content){
        deleteSlotbyId(currentId);
        get_or_create(name, size, var_args, content);
    }

    public void updateSlotbyName(String currentName, String name, List<Integer> size, HashMap<String, String> var_args, SlotContent content){
        deleteSlotbyName(currentName);
        get_or_create(name, size, var_args, content);
    }

    public SlotContent createSlotContent(String html, String handler, List<Slot> children, HashMap<String,Object> constant){
        SlotContent slotContent = new SlotContent();
        slotContent.setChildren(children);
        slotContent.setConstant(constant);
        slotContent.setHandler(handler);
        slotContent.setHtml(html);
        
		return slotContent;
    }

	public List<String> getAllNames() {
        List<Slot> allSlots = getAll();
        List<String> namesSlots = new ArrayList<String>();
        for (Slot slot : allSlots) {
        	namesSlots.add(slot.getName());
		}
        return namesSlots;
	}
}
